# coding=utf-8
'''
Created on 2011-12-10

@author: jiangxd
'''
import urllib
from xml.dom.minidom import parse, parseString

class Translator:
	BASE_URI = "http://api.microsofttranslator.com/V2/Http.svc/Translate"
	APP_ID = "D1600A22AE4F0465E0B22FD88954E4EB4FEADC30"

	def translate(self, frm, to, phrase):
		url = self.BASE_URI + "appId=" + self.APP_ID + "&from=" + frm + "&to=" + to + "&text=" + phrase
		args = {
					'appId': self.APP_ID,
					'from': frm,
					'to': to,
					'text': phrase
	        }
		argstring = urllib.urlencode(args)
		url = '%s?%s' % (self.BASE_URI, argstring)
		f = urllib.urlopen(url)
		dom = parseString(f.read())
		if dom.documentElement.tagName != 'string': return None
		return dom.documentElement.childNodes[0].data

if __name__ == '__main__':
	trans = Translator()
	print trans.translate('zh-CN', 'en', '你好')
